package com.hmily.dubbo.consumer.sync;

import com.hmily.dubbo.api.UserUpdateFacade;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;

import java.io.IOException;

/**
 * <h1>Zookeeper 动态下线操作。</h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2022-04-18
 *
 */
public class JavaCodeZkOfflineSvrConsumerApplication {

    public static void main(String[] args) throws IOException {
        // 设置应用服务名称
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-consumer");
        // 设置注册中心的地址
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");

        // 创建引用 UserUpdateFacade 这个服务的对象
        ReferenceConfig<UserUpdateFacade> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(UserUpdateFacade.class);

        // 直接拿到引用的代理对象，然后进行远程调用
        UserUpdateFacade userUpdateFacade = referenceConfig.get();
        System.out.println(userUpdateFacade.updateUserById("G314", "yuki"));

        startSubThread();

        // 阻塞等待
        System.in.read();
    }

    private static void startSubThread() {
        new Thread(() -> startSubThreadInner()).start();
    }

    private static void startSubThreadInner() {
        // 先睡眠 3s 时间，等待主线程逻辑都正常执行完
        sleepMills(3000);

        // 获取服务注册中心工厂对象
        RegistryFactory registryFactory =
                ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
        Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://127.0.0.1:2181"));

        // 直接对服务进行下线，即 override 操作，开启下线操作
        registry.register(URL.valueOf("override://192.168.43.183/com.hmily.dubbo.api.UserUpdateFacade?" +
                "dynamic=true&category=configurators&disabled=true"));
        sleepMills(2000);

        // 再次检测一下，是否还能调用通？
        ReferenceConfig<UserUpdateFacade> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(new ApplicationConfig("dubbo-consumer"));
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        referenceConfig.setInterface(UserUpdateFacade.class);
        try {
            System.out.println("禁用服务后的结果：" + referenceConfig.get().updateUserById("G1111", "hello"));
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("禁用服务后的尝试调用出现异常：" + e.getMessage());
        }

        // 取消下线，等会理论上应该是能再次调用服务拿到结果的
        registry.unregister(URL.valueOf("override://192.168.43.183/com.hmily.dubbo.api.UserUpdateFacade?" +
                "dynamic=true&category=configurators&disabled=true"));
        sleepMills(2000);
        ReferenceConfig<UserUpdateFacade> referenceConfig1 = new ReferenceConfig<>();
        referenceConfig1.setApplication(new ApplicationConfig("dubbo-consumer"));
        referenceConfig1.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        referenceConfig1.setInterface(UserUpdateFacade.class);
        try {
            System.out.println("取消禁用服务后的结果：" + referenceConfig1.get().updateUserById("G1212", "hmily"));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private static void sleepMills(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
